home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WINGs / wpanel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-09  |  11.0 KB  |  437 lines

  1.  
  2.  
  3. #include "WINGsP.h"
  4.  
  5. #include <X11/keysym.h>
  6.  
  7.  
  8.  
  9. static void
  10. alertPanelOnClick(WMWidget *self, void *clientData)
  11. {
  12.     WMAlertPanel *panel = clientData;
  13.  
  14.     panel->done = 1;
  15.     if (self == panel->defBtn) {
  16.     panel->result = WAPRDefault;
  17.     } else if (self == panel->othBtn) {
  18.     panel->result = WAPROther;
  19.     } else if (self == panel->altBtn) {
  20.     panel->result = WAPRAlternate;
  21.     }
  22. }
  23.  
  24.  
  25. static void
  26. handleKeyPress(XEvent *event, void *clientData)
  27. {
  28.     WMAlertPanel *panel = (WMAlertPanel*)clientData;
  29.  
  30.     if (event->xkey.keycode == panel->retKey && panel->defBtn) {
  31.     WMPerformButtonClick(panel->defBtn);
  32.     }
  33.     if (event->xkey.keycode == panel->escKey) {
  34.     if (panel->altBtn || panel->othBtn) {
  35.         WMPerformButtonClick(panel->othBtn ? panel->othBtn : panel->altBtn);
  36.     } else {
  37.         panel->result = WAPRDefault;
  38.         panel->done=1;
  39.     }
  40.     }
  41. }
  42.  
  43.  
  44. int 
  45. WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner,
  46.         char *title, char *msg, char *defaultButton,
  47.         char *alternateButton, char *otherButton)
  48. {
  49.     WMAlertPanel *panel;
  50.     int tmp;
  51.  
  52.     panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton,
  53.                    alternateButton, otherButton);
  54.  
  55.     scrPtr->modalView = W_VIEW(panel->win);
  56.     WMMapWidget(panel->win);
  57.  
  58.     scrPtr->modal = 1;
  59.     while (!panel->done || WMScreenPending(scrPtr)) {
  60.     XEvent event;
  61.     
  62.     WMNextEvent(scrPtr->display, &event);
  63.     WMHandleEvent(&event);
  64.     }
  65.     scrPtr->modal = 0;
  66.  
  67.     tmp = panel->result;
  68.  
  69.     WMDestroyAlertPanel(panel);
  70.     
  71.     return tmp;
  72. }
  73.  
  74.  
  75. void
  76. WMDestroyAlertPanel(WMAlertPanel *panel)
  77. {
  78.     WMUnmapWidget(panel->win);
  79.     WMDestroyWidget(panel->win);
  80.     wfree(panel);
  81. }
  82.  
  83.  
  84. WMAlertPanel* 
  85. WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner, 
  86.            char *title, char *msg, char *defaultButton,
  87.            char *alternateButton, char *otherButton)
  88. {
  89.     WMAlertPanel *panel;
  90.     int x, dw=0, aw=0, ow=0, w;
  91.     
  92.     
  93.     panel = wmalloc(sizeof(WMAlertPanel));
  94.     memset(panel, 0, sizeof(WMAlertPanel));
  95.  
  96.         
  97.     panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
  98.     panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
  99.  
  100.     if (owner)
  101.     panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
  102.                              WMTitledWindowMask);
  103.     else
  104.     panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
  105.                          WMTitledWindowMask);
  106.  
  107.     WMSetWindowInitialPosition(panel->win,
  108.      (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
  109.      (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
  110.  
  111.     WMSetWindowTitle(panel->win, "");
  112.  
  113.     if (scrPtr->applicationIcon) {        
  114.     panel->iLbl = WMCreateLabel(panel->win);
  115.     WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
  116.                scrPtr->applicationIcon->height);
  117.     WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
  118.              (75 - scrPtr->applicationIcon->height)/2);
  119.     WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
  120.     WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
  121.     }
  122.  
  123.     if (title) {
  124.     WMFont *largeFont;
  125.     
  126.     largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
  127.  
  128.     panel->tLbl = WMCreateLabel(panel->win);
  129.     WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
  130.     WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
  131.     WMSetLabelText(panel->tLbl, title);
  132.     WMSetLabelTextAlignment(panel->tLbl, WALeft);
  133.     WMSetLabelFont(panel->tLbl, largeFont);
  134.     
  135.     WMReleaseFont(largeFont);
  136.     }
  137.  
  138.  
  139.     if (msg) {
  140.     panel->mLbl = WMCreateLabel(panel->win);
  141.     WMMoveWidget(panel->mLbl, 10, 83);
  142.     WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
  143.     WMSetLabelText(panel->mLbl, msg);
  144.     WMSetLabelTextAlignment(panel->mLbl, WACenter);
  145.     }
  146.     
  147.     
  148.     /* create divider line */
  149.     
  150.     panel->line = WMCreateFrame(panel->win);
  151.     WMMoveWidget(panel->line, 0, 80);
  152.     WMResizeWidget(panel->line, 400, 2);
  153.     WMSetFrameRelief(panel->line, WRGroove);
  154.  
  155.     /* create buttons */
  156.     if (otherButton) 
  157.     ow = WMWidthOfString(scrPtr->normalFont, otherButton, 
  158.                  strlen(otherButton));
  159.     
  160.     if (alternateButton)
  161.     aw = WMWidthOfString(scrPtr->normalFont, alternateButton, 
  162.                  strlen(alternateButton));
  163.     
  164.     if (defaultButton)
  165.     dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
  166.                  strlen(defaultButton));
  167.     
  168.     w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
  169.     if (aw > w)
  170.     w = aw;
  171.     if (ow > w)
  172.     w = ow;
  173.  
  174.     w += 30;
  175.     x = 400;
  176.  
  177.     if (defaultButton) {
  178.     x -= w + 10;
  179.     
  180.     panel->defBtn = WMCreateCommandButton(panel->win);
  181.     WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
  182.     WMMoveWidget(panel->defBtn, x, 144);
  183.     WMResizeWidget(panel->defBtn, w, 24);
  184.     WMSetButtonText(panel->defBtn, defaultButton);
  185.     WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
  186.     WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
  187.     WMSetButtonImagePosition(panel->defBtn, WIPRight);
  188.     }
  189.     if (alternateButton) {
  190.     x -= w + 10;
  191.  
  192.     panel->altBtn = WMCreateCommandButton(panel->win);
  193.     WMMoveWidget(panel->altBtn, x, 144);
  194.     WMResizeWidget(panel->altBtn, w, 24);
  195.     WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
  196.     WMSetButtonText(panel->altBtn, alternateButton);
  197.     }
  198.     if (otherButton) {
  199.     x -= w + 10;
  200.     
  201.     panel->othBtn = WMCreateCommandButton(panel->win);
  202.     WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
  203.     WMMoveWidget(panel->othBtn, x, 144);
  204.     WMResizeWidget(panel->othBtn, w, 24);
  205.     WMSetButtonText(panel->othBtn, otherButton);    
  206.     }
  207.  
  208.     panel->done = 0;
  209.     
  210.     WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
  211.              handleKeyPress, panel);
  212.  
  213.     WMRealizeWidget(panel->win);
  214.     WMMapSubwidgets(panel->win);
  215.  
  216.     return panel;
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. static void
  224. inputBoxOnClick(WMWidget *self, void *clientData)
  225. {
  226.     WMInputPanel *panel = clientData;
  227.  
  228.     panel->done = 1;
  229.     if (self == panel->defBtn) {
  230.     panel->result = WAPRDefault;
  231.     } else if (self == panel->altBtn) {
  232.     panel->result = WAPRAlternate;
  233.     }
  234. }
  235.  
  236.  
  237.  
  238. static void
  239. handleKeyPress2(XEvent *event, void *clientData)
  240. {
  241.     WMInputPanel *panel = (WMInputPanel*)clientData;
  242.  
  243.     if (event->xkey.keycode == panel->retKey && panel->defBtn) {
  244.     WMPerformButtonClick(panel->defBtn);
  245.     }
  246.     if (event->xkey.keycode == panel->escKey) {
  247.     if (panel->altBtn) {
  248.             WMPerformButtonClick(panel->altBtn);
  249.     } else {
  250.         /*           printf("got esc\n");*/
  251.         panel->done = 1;
  252.         panel->result = WAPRDefault;
  253.     }
  254.     }
  255. }
  256.  
  257.  
  258. char*
  259. WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, 
  260.         char *msg, char *defaultText,
  261.         char *okButton, char *cancelButton)
  262. {
  263.     WMInputPanel *panel;
  264.     char *tmp;
  265.  
  266.     panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText, 
  267.                    okButton, cancelButton);
  268.  
  269.     WMMapWidget(panel->win);
  270.  
  271.     while (!panel->done || WMScreenPending(scrPtr)) {
  272.     XEvent event;
  273.     
  274.     WMNextEvent(scrPtr->display, &event);
  275.     WMHandleEvent(&event);
  276.     }
  277.  
  278.  
  279.     if (panel->result == WAPRDefault)
  280.     tmp = WMGetTextFieldText(panel->text);
  281.     else
  282.     tmp = NULL;
  283.  
  284.     WMDestroyInputPanel(panel);
  285.  
  286.     return tmp;
  287. }
  288.  
  289.  
  290. void
  291. WMDestroyInputPanel(WMInputPanel *panel)
  292. {
  293.     WMRemoveNotificationObserver(panel);
  294.     WMUnmapWidget(panel->win);
  295.     WMDestroyWidget(panel->win);
  296.     wfree(panel);
  297. }
  298.  
  299.  
  300.  
  301. static void
  302. endedEditingObserver(void *observerData, WMNotification *notification)
  303. {
  304.     WMInputPanel *panel = (WMInputPanel*)observerData;
  305.     
  306.     switch ((int)WMGetNotificationClientData(notification)) {
  307.      case WMReturnTextMovement:
  308.     if (panel->defBtn)
  309.         WMPerformButtonClick(panel->defBtn);
  310.     break;
  311.      case WMEscapeTextMovement:
  312.     if (panel->altBtn)
  313.         WMPerformButtonClick(panel->altBtn);
  314.     else {
  315.         panel->done = 1;
  316.         panel->result = WAPRDefault;
  317.     }
  318.     break;
  319.      default:
  320.     break;
  321.     }
  322. }
  323.  
  324.  
  325. WMInputPanel*
  326. WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
  327.            char *defaultText, char *okButton, char *cancelButton)
  328. {
  329.     WMInputPanel *panel;
  330.     int x, dw=0, aw=0, w;
  331.     
  332.     
  333.     panel = wmalloc(sizeof(WMInputPanel));
  334.     memset(panel, 0, sizeof(WMInputPanel));
  335.  
  336.     panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
  337.     panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
  338.  
  339.     if (owner)
  340.     panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
  341.                              WMTitledWindowMask);
  342.     else
  343.     panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
  344.                          WMTitledWindowMask);
  345.     WMSetWindowTitle(panel->win, "");
  346.  
  347.     WMResizeWidget(panel->win, 320, 160);
  348.     
  349.     if (title) {
  350.     WMFont *largeFont;
  351.     
  352.     largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
  353.     
  354.     panel->tLbl = WMCreateLabel(panel->win);
  355.     WMMoveWidget(panel->tLbl, 20, 16);
  356.     WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
  357.     WMSetLabelText(panel->tLbl, title);
  358.     WMSetLabelTextAlignment(panel->tLbl, WALeft);
  359.     WMSetLabelFont(panel->tLbl, largeFont);
  360.     
  361.     WMReleaseFont(largeFont);
  362.     }
  363.  
  364.  
  365.     if (msg) {
  366.     panel->mLbl = WMCreateLabel(panel->win);
  367.     WMMoveWidget(panel->mLbl, 20, 50);
  368.     WMResizeWidget(panel->mLbl, 320 - 40, 
  369.                WMFontHeight(scrPtr->normalFont)*2);
  370.     WMSetLabelText(panel->mLbl, msg);
  371.     WMSetLabelTextAlignment(panel->mLbl, WALeft);
  372.     }
  373.     
  374.     panel->text = WMCreateTextField(panel->win);
  375.     WMMoveWidget(panel->text, 20, 85);
  376.     WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
  377.     WMSetTextFieldText(panel->text, defaultText);
  378.  
  379.     WMAddNotificationObserver(endedEditingObserver, panel, 
  380.                   WMTextDidEndEditingNotification, panel->text);
  381.  
  382.     /* create buttons */
  383.     if (cancelButton)
  384.     aw = WMWidthOfString(scrPtr->normalFont, cancelButton, 
  385.                  strlen(cancelButton));
  386.     
  387.     if (okButton)
  388.     dw = WMWidthOfString(scrPtr->normalFont, okButton,
  389.                  strlen(okButton));
  390.     
  391.     w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
  392.     if (aw > w)
  393.     w = aw;
  394.  
  395.     w += 30;
  396.     x = 310;
  397.  
  398.     if (okButton) {
  399.     x -= w + 10;
  400.  
  401.     panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
  402.                          |WBBPushChangeMask
  403.                          |WBBPushLightMask);
  404.     WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
  405.     WMMoveWidget(panel->defBtn, x, 124);
  406.     WMResizeWidget(panel->defBtn, w, 24);
  407.     WMSetButtonText(panel->defBtn, okButton);
  408.     WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
  409.     WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
  410.     WMSetButtonImagePosition(panel->defBtn, WIPRight);
  411.     }
  412.     if (cancelButton) {
  413.     x -= w + 10;
  414.  
  415.     panel->altBtn = WMCreateCommandButton(panel->win);
  416.     WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
  417.     WMMoveWidget(panel->altBtn, x, 124);
  418.     WMResizeWidget(panel->altBtn, w, 24);
  419.     WMSetButtonText(panel->altBtn, cancelButton);
  420.     }
  421.  
  422.     panel->done = 0;
  423.     
  424.     WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
  425.              handleKeyPress2, panel);
  426.  
  427.     WMRealizeWidget(panel->win);
  428.     WMMapSubwidgets(panel->win);
  429.  
  430.     WMSetFocusToWidget(panel->text);
  431.  
  432.     return panel;
  433. }
  434.  
  435.  
  436.  
  437.